<template>
  <div class="ai-chat-container">
    <div class="chat-window" ref="chatWindow">
      <div v-for="(msg, idx) in messages" :key="idx" :class="['message', msg.role]">
        <span v-if="msg.role === 'user'">🧑‍💻：</span>
        <span v-if="msg.role === 'ai'">🤖：</span>
        <span>{{ msg.content }}</span>
      </div>
      <div v-if="isStreaming" class="message ai">
        <span>🤖：</span><span>{{ streamingContent }}</span><span class="cursor">|</span>
      </div>
    </div>
    <div class="input-area">
      <input v-model="input" @keyup.enter="sendMessage" placeholder="请输入内容..." />
      <button @click="sendMessage" :disabled="isStreaming || !input.trim()">发送</button>
    </div>
  </div>
</template>

<script setup>
import { ref, nextTick } from 'vue'

const input = ref('')
const messages = ref([])
const isStreaming = ref(false)
const streamingContent = ref('')
const chatWindow = ref(null)
let eventSource = null

function sendMessage() {
  if (!input.value.trim() || isStreaming.value) return
  const content = input.value.trim()
  messages.value.push({ role: 'user', content })
  input.value = ''
  startSSE(content)
}

function startSSE(content) {
  isStreaming.value = true
  streamingContent.value = ''
  
  // 关闭之前的连接
  if (eventSource) {
    eventSource.close()
    eventSource = null
  }
  
  // 创建新的SSE连接
  eventSource = new EventSource(`http://localhost:9000/ai/sse/time`)
  
  // 处理消息
  eventSource.onmessage = function(event) {
    streamingContent.value += event.data
    scrollToBottom()
  }
  
  // 处理错误
  eventSource.onerror = function() {
    console.log("SSE连接断开")
    finishStreaming()
    eventSource.close()
  }
  
  // 连接打开
  eventSource.onopen = function() {
    console.log("SSE连接已建立")
  }
}

function finishStreaming() {
  if (streamingContent.value.trim()) {
    messages.value.push({ role: 'ai', content: streamingContent.value })
  }
  isStreaming.value = false
  streamingContent.value = ''
  scrollToBottom()
}

function scrollToBottom() {
  nextTick(() => {
    if (chatWindow.value) {
      chatWindow.value.scrollTop = chatWindow.value.scrollHeight
    }
  })
}

// 组件卸载时清理连接
import { onUnmounted } from 'vue'
onUnmounted(() => {
  if (eventSource) {
    eventSource.close()
    eventSource = null
  }
})
</script>

<style scoped>
.ai-chat-container {
  max-width: 500px;
  margin: 40px auto;
  border: 1px solid #eee;
  border-radius: 8px;
  box-shadow: 0 2px 8px #eee;
  background: #fff;
  display: flex;
  flex-direction: column;
  height: 600px;
}

.chat-window {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  font-size: 16px;
  scroll-behavior: smooth;
}

.message {
  margin-bottom: 12px;
  line-height: 1.7;
  word-wrap: break-word;
}

.message.user {
  text-align: right;
  color: #409eff;
}

.message.ai {
  text-align: left;
  color: #333;
}

.input-area {
  display: flex;
  border-top: 1px solid #eee;
  padding: 12px;
  background: #fafbfc;
}

.input-area input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  outline: none;
}

.input-area input:focus {
  border-color: #409eff;
}

.input-area button {
  margin-left: 8px;
  padding: 8px 20px;
  font-size: 16px;
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.2s;
}

.input-area button:hover:not(:disabled) {
  background: #337ecc;
}

.input-area button:disabled {
  background: #bcdcff;
  cursor: not-allowed;
}

.cursor {
  display: inline-block;
  width: 8px;
  animation: blink 1s steps(1) infinite;
}

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}
</style> 